import axios from 'axios'
import storage from '../utils/storeUtils'
import {TOKENKEY} from './constant'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({
    easing: "ease-in-out",
    speed: 300,
    trickeSpeed: 300,
 })
const service=axios.create({
    baseURL:import.meta.env.VITE_BASE_URL,
    timeout:5000
})
service.interceptors.request.use(config=>{
    NProgress.start()
    config.headers['Content-Type']="application/x-www-form-urlencoded"
    const token=storage.getStorage(TOKENKEY)
    if(token){
        config.headers.Authentication=token
    }
    return config
},err=>{
    return Promise.reject(err)
})
service.interceptors.response.use(response=>{
    NProgress.done()
    return response.data
},err=>{
    if(err.response){
        switch(err.response.status){
            case 401:
               console.log('---401--------------');
                //清空token
                localStorage.removeItem('token')
                localStorage.removeItem('user')
                window.location.href="/login"
                break;
            case 500:
                break
        }
    }
    return Promise.reject(err)
})
export default service